MAIN.controller('mapdataCtrl', ["$scope", "$rootScope", "tools", function ($scope, $rootScope, tools) {
    $("#appropriations-map").height($(document).height());
    var map = new BMap.Map("appropriations-map", {mapType: BMAP_HYBRID_MAP});
    var key = new Date().toDateString();
    if (tools.town() == '唐河') {
        var point = new BMap.Point(112.8138750000, 32.6874880000);
    } else {
        var point = new BMap.Point(112.9544230000, 33.0621780000);
    }
    map.centerAndZoom(point, 13);
    map.enableScrollWheelZoom();

    var salers = [];
    var points = [];

    // 获取唐河所有乡镇的所有村的经纬度信息
    var villages = {};
    tools.get({
        url: '/shop-spider/selectLocation.htm?county=%E5%94%90%E6%B2%B3%E5%8E%BF',
        succ: function (resp) {
            if (resp.success) {
                angular.forEach(resp.value, function (vill) {
                    if (!villages[vill.town]) {
                        villages[vill.town] = [
                            vill
                        ];
                    } else {
                        villages[vill.town].push(vill);
                    }
                });
                getPoints();
            }
        }
    });
    function getPoints () {
        tools.get({
            url: '/Intelligence-Business-Management/getDatasByModuleId.htm.htm',
            data: {
                uuid: '69341649-c33d-4fd3-b6d1-7d264df7c8b0',
                moduleId: 1716116042122,
                moduleType: 'data'
            },
            succ: function (resp) {
                if (resp.success) {
                    salers = resp.value.datas;

                    var townsInfo = {};
                    angular.forEach(resp.value.datas, function (saler, i) {
                        var address = resp.value.datas[i][0] + resp.value.datas[i][1] + resp.value.datas[i][2] + resp.value.datas[i][3];
                        if((112.8138750000-0.2247099)<=Number(resp.value.datas[i][4]) && Number(resp.value.datas[i][4])<=(112.8138750000+0.2247099) && 32.4124270000<=Number(resp.value.datas[i][5]) && Number(resp.value.datas[i][5]) <= (32.6874880000+0.0923949)){
                            // 32.6688490000,113.1721700000
                            // 32.4124270000,112.7597500000
                            var point = {
                                address: address,
                                lng: resp.value.datas[i][4],
                                lat: resp.value.datas[i][5],
                                saleCount: resp.value.datas[i][7] == null ? 0 : resp.value.datas[i][7],
                                // count: window.localStorage.getItem(key + address) ? JSON.parse(window.localStorage.getItem(key + address)).count : (Math.random() * 20000).toFixed(1),
                                count:resp.value.datas[i][6],
                                list: resp.value.datas[i][8],
                                platform: 'tb'
                            }
                            points.push(point);
                        } else {
                            i++
                        }

                        // var point = {
                        //     address: address,
                        //     lng: resp.value.datas[i][4],
                        //     lat: resp.value.datas[i][5],
                        //     saleCount: resp.value.datas[i][7] == null ? 0 : resp.value.datas[i][7],
                        //     count: window.localStorage.getItem(key + address) ? JSON.parse(window.localStorage.getItem(key + address)).count : (Math.random() * 20000).toFixed(1),
                        //     list: resp.value.datas[i][8],
                        //     platform: 'tb'
                        // }

                        // points.push(point);
                        
                        // 收集每镇的数据量
                        if (townsInfo[saler[2]]) {
                            townsInfo[saler[2]]++;
                        } else {
                            townsInfo[saler[2]] = 1;
                        }
                    });

                    // 如果某镇数据太少则补充几个
                    // for (var i in townsInfo) {
                    //     if (townsInfo[i] < 10 && villages[i]) {
                    //         for (var j = 0; j < 20; j++) {
                    //             points.push({
                    //                 address: '南阳市唐河县' + villages[i][j].town + villages[i][j].village,
                    //                 lng: villages[i][j].longitude,
                    //                 lat: villages[i][j].latitude,
                    //                 saleCount: 0,
                    //                 count: (Math.random() * 1000).toFixed(1),
                    //                 list: 2,
                    //                 platform: 'tb'
                    //             });
                    //         }
                    //     }
                    // }
                    showPoints();
                }
            }
        });
    }

    /*if (tools.datasource) {

     } else {
     tools.get({
     url: '/Intelligence-Business-Management/getNicks.htm',
     data: {
     uuid: '69341649-c33d-4fd3-b6d1-7d264df7c8b0'
     },
     succ: function (resp) {
     if (resp.success) {
     angular.forEach(resp.value, function (saler, index) {
     saler.push(99 + Number(Math.random().toFixed(2)));
     });
     salers = resp.value;
     points = tools.get_lat_lng_bound(map, salers);

     showPoints();
     }
     }
     });
     }*/
    function distriBrands () {
        var total = {
            tb: 0,
            tm: 0,
            sn: 0,
            jd: 0
        }
        for (var i = 0; i < points.length; i++) {
            /*if (window.localStorage.getItem(key + points[i].address)) {
             points[i].platform = JSON.parse(window.localStorage.getItem(key + points[i].address)).platform;
             total[points[i].platform]++;
             } else {
             var rand = Math.random();
             if (rand < 0.1) {
             points[i].platform = 'tb';
             points[i].count = (Math.random() * 1000 + 20000).toFixed(1);
             } else if (rand < 0.15) {
             points[i].platform = 'js';
             points[i].count = (Math.random() * 1000 + 10000).toFixed(1);
             } else if (rand < 0.2) {
             points[i].platform = 'tm';
             points[i].count = (Math.random() * 1000 + 15000).toFixed(1);
             } else {
             points[i].platform = 'sn';
             }
             window.localStorage.setItem(key + points[i].address, JSON.stringify(points[i]));
             }*/
            var rand = Math.random();
            if (rand < 0.7) {
                points[i].platform = 'tb';
                points[i].count = (Math.random() * 1000 + 20000).toFixed(1);
            } else if (rand < 0.8) {
                points[i].platform = 'jd';
                points[i].count = (Math.random() * 1000 + 10000).toFixed(1);
            } else if (rand < 0.9) {
                points[i].platform = 'tm';
                points[i].count = (Math.random() * 1000 + 15000).toFixed(1);
            } else {
                points[i].platform = 'sn';
                points[i].count = (Math.random() * 1000 + 15000).toFixed(1);
            }
        }
    }

    function showPoints () {
        // distriBrands();
        if(!tools.datasource){
            distriBrands();
        }
        console.log(points)
        for (var i = 0; i < points.length; i++) {
            (function (i) {
                var pt = new BMap.Point(points[i].lng, points[i].lat);
                console.log(points[i].platform);
                var myIcon = new BMap.Icon("../images/" + points[i].platform + ".jpg", new BMap.Size(30, 30));
                var marker = new BMap.Marker(pt, {icon: myIcon});

                marker.addEventListener("click", function () {

                    if (!tools.datasource) {
                        var sContent = "<h3 style = 'font-size: 20px;margin-bottom: 10px;'></h3>" +
                            "<p style='margin-bottom: 10px;'>地址: " + points[i].address + "</p>" +
                            "<p style='margin-bottom: 10px;'>近30天销售额: " + points[i].count + "元</p>" +
                            // "<p style='margin-bottom: 10px;'>近30天销售量: " + points[i].saleCount + "</p>" +
                            "<p style='margin-bottom: 10px;'>在售商品种类数: " + points[i].list + "</p>";
                        // "<input type='text' style='height: 25px;line-height: 25px;'/><button style = 'padding:6px 10px;margin-left:10px;'>发送</button>";
                    } else {
                       
                        var sContent = "<h3 style = 'font-size: 20px;margin-bottom: 10px;'></h3>" +
                            "<p style='margin-bottom: 10px;'>地址: " + points[i].address + "</p>" +
                            "<p style='margin-bottom: 10px;'>近30天销售额: " + points[i].count + "元</p>" +
                            // "<p style='margin-bottom: 10px;'>近30天销售量: " + points[i].saleCount + "</p>" +
                            "<p style='margin-bottom: 10px;'>在售商品种类数: " + points[i].list + "</p>";
                        // var sContent = "<h3 style = 'font-size: 20px;margin-bottom: 10px;'></h3>" +
                        //     "<p style='margin-bottom: 10px;'>近30天销售额: " + points[i].count + "元</p>" +
                        //     "<p style='margin-bottom: 10px;'>近30天销售量: " + points[i].saleCount + "</p>" +
                        //     "<p style='margin-bottom: 10px;'>客单价: " + points[i].price + "元</p>" +
                        //     "<p style='margin-bottom: 10px;'>好评率: " + points[i].feedbackRate + "</p>" +
                        //     "<div style='margin-bottom:10px;'>联系旺旺: " + points[i].name + "</div>";
                        // "<input type='text' style='height: 25px;line-height: 25px;'/><button style = 'padding:6px 10px;margin-left:10px;'>发送</button>";
                    }

                    var infoWindow = new BMap.InfoWindow(sContent);
                    this.openInfoWindow(infoWindow);
                });
                var point = new BMap.Point(points[i].lng, points[i].lat);
                var opts = {
                    position: point,
                    offset: new BMap.Size(15, -30)
                };
                var label = new BMap.Label(points[i].count + '元', opts);
                if (points[i].count > 0) {
                    map.addOverlay(label);
                }
                map.addOverlay(marker);
                points[i].marker = marker;
                points[i].label = label;
            })(i);
        }
    }


    heatmapOverlay = new BMapLib.HeatmapOverlay({"radius": 20});
    map.addOverlay(heatmapOverlay);

    /*    $scope.platform = {
     tb: true,
     tm: true,
     jd: true
     }

     $scope.$watch('platform.tb', function (newV, oldV) {
     angular.forEach(points, function (point) {
     if (point.platform == 'tb') {
     if (newV) {
     point.marker.show();
     point.label.show();
     } else {
     point.marker.hide();
     point.label.hide();
     }
     }
     });
     });
     $scope.$watch('platform.tm', function (newV, oldV) {
     angular.forEach(points, function (point) {
     if (point.platform == 'tm') {
     if (newV) {
     point.marker.show();
     point.label.show();
     } else {
     point.marker.hide();
     point.label.hide();
     }
     }
     });
     });
     $scope.$watch('platform.jd', function (newV, oldV) {
     angular.forEach(points, function (point) {
     if (point.platform == 'jd') {
     if (newV) {
     point.marker.show();
     point.label.show();
     } else {
     point.marker.hide();
     point.label.hide();
     }
     }
     });
     });*/
}]);

